import 'package:flutter/material.dart';

import 'package:charts_flutter/flutter.dart' as charts;
import 'package:drm/base/tj_date_entity.dart';


class ThreePage extends StatefulWidget {
  List<TjDateEntity> data;

  ThreePage({Key key, this.data}) : super(key: key);

  @override
  _ThreePageState createState() => _ThreePageState();
}

class _ThreePageState extends State<ThreePage> {

  @override
  Widget build(BuildContext context) {
    return charts.BarChart(
      _createSampleData(),
      animate: true,
      vertical: false,
      barRendererDecorator: new charts.BarLabelDecorator<String>(),

//      behaviors: [new charts.SeriesLegend(
//        position: charts.BehaviorPosition.top,
//
//      )],
    );

  }

  charts.Series<TjDateEntity, String> iniRow(String title,List<TjDateEntity> data) {
    return charts.Series<TjDateEntity, String>(
      id: title,
      domainFn: (TjDateEntity sales, _) {
        if (sales.name.length > 6) {
          return sales.name.substring(0,6)+'...';
        }
        return sales.name;
      },
      measureFn: (TjDateEntity sales, _) {
        try {
          if (sales.value.isEmpty) {
            return 0 ;
          }else {
            return double.parse(sales.value);
          }

        } catch (e) {
          return 0;
        }
      },
      data: data,
      labelAccessorFn: (TjDateEntity sales, _) => '${sales.value.isEmpty?'0':sales.value}',

    );
  }

  List<charts.Series<TjDateEntity, String>> _createSampleData() {

    return [
      iniRow('订货数量',widget.data),
    ];
  }
}
